<?php


	 
	
class MemberView {

	private $m_firstName = 'm_firstName';
	private $m_lastName = 'm_lastName';
	private $m_userName = 'm_userName';
	private $m_password = 'm_password';
	private $m_mail = 'm_mail';
	private $m_checkPassword = 'checkPassword';
	private $m_ssn = 'm_ssn';
	private $m_beMember = 'becomeMember';
	private $m_deleteMember = 'm_deleteMember';
	private $m_validate;
	
	const EMPTY_FIRSTNAME = "EMPTY_FIRSTNAME";
	const EMPTY_LASTNAME = "EMPTY_LASTNAME";
	const EMPTY_MAIL = "EMPTY_MAIL";
	const EMPTY_USERNAME = "EMPTY_USERNAME";
	const EMPTY_SSN = "EMPTY_SSN";
	const EMPTY_PASSWORD = "EMPTY_PASSWORD";
	const NO_MATCH_PASSWORD = "NO_MATCH_PASSWORD";
	const INCORRECT_NAME = "INCORRECT_NAME";
	const INCORRECT_MAIL = "INCORRECT_MAIL";
	const INCORRECT_USERNAME = "INCORRECT_USERNAME";
	const INCORRECT_SSN = "INCORRECT_SSN";
	const INCORRECT_PASSWORD = "INCORRECT_PASSWORD";
	const USERNAME_EXISTS = "USERNAME_EXISTS";
	const SSN_EXISTS = "SSN_EXISTS";
	
	public function __construct($a_validate) {
		$this-> m_validate = $a_validate;
	}

	private $m_firstNameError = "";
	private $m_lastNameError = "";
	private $m_userNameError = "";
	private $m_passwordError = "";
	private $m_mailError = "";
	private $m_ssnError = "";

	//Lots and lots of errorhandling, it's not messy, but it's alot.
	public function HasError() {
		$errorsMessages = self::ErrorMessage();
		$errors = false;
		
		if ($_POST[$this -> m_firstName] == "") {
			$this->m_firstNameError = $errorsMessages[self::EMPTY_FIRSTNAME];
			$errors = true;
		}elseif (!$this->m_validate->nameString($_POST[$this -> m_firstName])){
			$this->m_firstNameError = $errorsMessages[self::INCORRECT_NAME];
			$errors = true;
		}
		
		if ($_POST[$this -> m_lastName] == "") {
			$this->m_lastNameError = $errorsMessages[self::EMPTY_LASTNAME];
			$errors = true;
		}elseif (!$this->m_validate->nameString($_POST[$this -> m_lastName])){
			$this->m_lastNameError = $errorsMessages[self::INCORRECT_NAME];
			$errors = true;
		}
		
		if ($_POST[$this -> m_userName] == "") {
			$this->m_userNameError = $errorsMessages[self::EMPTY_USERNAME];
			$errors = true;
		}elseif (!$this->m_validate->UserName($_POST[$this -> m_userName])){
			$this->m_userNameError = $errorsMessages[self::INCORRECT_USERNAME];
			$errors = true;
		}

		if ($_POST[$this -> m_password] == "") {
			$this->m_passwordError = $errorsMessages[self::EMPTY_PASSWORD];
			$errors = true;
		}elseif (!$this->m_validate->CheckPasswordFormat($_POST[$this -> m_password])){
			$this->m_passwordError = $errorsMessages[self::INCORRECT_PASSWORD];
			$errors = true;
		}elseif ($_POST[$this -> m_password] != $_POST[$this -> m_checkPassword]){
			$this->m_passwordError = $errorsMessages[self::NO_MATCH_PASSWORD];
			$errors = true;
		}
		
		if ($_POST[$this -> m_mail] == "") {
			$this->m_mailError = $errorsMessages[self::EMPTY_MAIL];
			$errors = true;
		}elseif (!$this->m_validate->email($_POST[$this -> m_mail])){
			$this->m_mailError = $errorsMessages[self::INCORRECT_MAIL];
			$errors = true;
		}
		
		if ($_POST[$this -> m_ssn] == "") {
			$this->m_ssnError = $errorsMessages[self::EMPTY_SSN];
			$errors = true;
		}elseif (!$this->m_validate->Ssn($_POST[$this -> m_ssn])){
			$this->m_ssnError = $errorsMessages[self::INCORRECT_SSN];
			$errors = true;
		}
		if ($errors == true){
			return true;
		}
		else {
			$_POST[$this -> m_ssn] = $this->m_validate->Ssn($_POST[$this -> m_ssn]);
			return false;
		}
	}
	
	// Gets info from becomemember form
	public function GetFirstName() {	
		return $_POST[$this -> m_firstName];
	}

	public function GetLastName() {
		return $_POST[$this -> m_lastName];
	}

	public function GetUserName() {
		return $_POST[$this -> m_userName];
	}

	public function GetPassword() {
		return $_POST[$this -> m_password];
	}

	public function GetCheckPassword() {
		return $_POST[$this -> m_checkPassword];
	}

	public function GetMail() {
		return $_POST[$this -> m_mail];
	}

	public function GetSsn() {
		return $_POST[$this -> m_ssn];
	}
	
	public function EmptyInput ($var){
		if($var == NULL){
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	//Becomemember errormessages
	public function ErrorMessage(){
		return array(self::EMPTY_FIRSTNAME =>"<p><span class='errortxt'>Du måste ange ett förnamn</span></p>", 
		self::EMPTY_LASTNAME => "<p><span class='errortxt'>Du måste ange ett efternamn</span></p>",
		self::EMPTY_MAIL =>"<p><span class='errortxt'>Du måste ange en mailadress</span></p>", 
		self::EMPTY_USERNAME =>  "<p><span class='errortxt'>Du måste ange ett användarnamn</span></p>",
		self::EMPTY_SSN =>"<p><span class='errortxt'>Du måste ange ett personnummer</span></p>",
		self::EMPTY_PASSWORD => "<p><span class='errortxt'>Du måste ange ett lösenord</span></p>",
		self::NO_MATCH_PASSWORD =>"<p><span class='errortxt'>Lösenorden matchar inte, försök igen</span></p>",
		self::INCORRECT_NAME =>"<p><span class='errortxt'>Felaktiga tecken. Använd a-ö och/eller bindestreck</span></p>",
		self::INCORRECT_MAIL=>"<p><span class='errortxt'>Felaktig mail-adress</span></p>",
		self::INCORRECT_USERNAME=>"<p><span class='errortxt'>Fel användarnamn</span></p>",
		self::INCORRECT_SSN =>"<p><span class='errortxt'>Ogiltigt personnummer</span></p>",
		self::INCORRECT_PASSWORD=>"<p><span class='errortxt'>Ange ett lösenord med minst sex tecken som innehåller minst en gemen, en versal och en siffra</span></p>",
		self::USERNAME_EXISTS=>"<p><span class='errortxt'>Användarnamnet är upptaget, välj ett nytt</span></p>",
		self::SSN_EXISTS =>"<p><span class='errortxt'>Det finns redan en användare registrerad med det personnumret</span></p>");
		}
	
	
	public function DoBecomeMemberForm() {
		return "
			 ";
			 /*
			  * <a href='/lab2/' title='startsidan'>Gå tillbaka till startsidan</a>
			<p>Fyll i användaruppgifter:</p>
			<form action='' method='post'>
			<p>Förnamn: <input type = 'text' name = '$this->m_firstName'/> </p>
			<p>Efternamn: <input type = 'text' name = '$this->m_lastName'/></p>
			<p>mail-adress: <input type = 'text' name = '$this->m_mail'/> </p>
			<p>Användarnamn: <input type='text' name ='$this->m_userName'/></p>
			<p>Personnummer: <input type='text' name = '$this->m_ssn'/></p>
			<p>Lösenord: <input type='password' name ='$this->m_password'/></p>
			<p>Upprepa lösenord <input type ='password' name = '$this->m_checkPassword'/> </p>
			<input type='submit' name='$this->m_beMember' value = 'Registrera medlem'/>
		</form>
			  */
	}

	public function ReFillDoBecomeMemberForm($userExists, $m_ssnExists) {
			$errorsMessages = self::ErrorMessage();
			$m_userNameError = "";
			$ssnError = "";
			
			if($this->m_userNameError != ""){
				$m_userNameError = $this->m_userNameError;
				
			}elseif ($userExists != ""){
				$m_userNameError = $errorsMessages[self::USERNAME_EXISTS];
			}
			
			if($this->m_ssnError != ""){
				$ssnError = $this->m_ssnError;
			}elseif ($m_ssnExists != ""){
				$ssnError = $errorsMessages[self::SSN_EXISTS];
			}
			
		return "
			<a href='/lab2/' title='startsidan'>Gå tillbaka till startsidan</a>
			<p>Fyll i användaruppgifterna igen:</p>
			<form action='' method='post'>
			<p>Förnamn: <input type = 'text' name = '$this->m_firstName' value='" . $_POST[$this -> m_firstName] . "'/> </p>
			 $this->m_firstNameError
			<p>Efternamn: <input type = 'text' name = '$this->m_lastName' value='" . $_POST[$this -> m_lastName] . "'/></p>
			 $this->m_lastNameError 
			<p>mail-adress: <input type = 'text' name = '$this->m_mail' value='" . $_POST[$this -> m_mail] . "'/> </p>
			$this->m_mailError
			<p>Användarnamn: <input type='text' name ='$this->m_userName' value='" . $_POST[$this -> m_userName] . "'/></p>
			$m_userNameError
			<p>Personnummer: <input type='text' name = '$this->m_ssn' value='" . $_POST[$this -> m_ssn] . "'/></p>
			$ssnError
			<p>Lösenord: <input type='password' name ='$this->m_password'/></p>
			$this->m_passwordError
			<p>Upprepa lösenord <input type = 'password' name = '$this->m_checkPassword'/> </p>
			<input type='submit' name='$this->m_beMember' value = 'Registrera medlem'/>
		</form> ";
	}

	public function DoQuitMembershipForm() {
		return "
		<div id='formContainer'><p>Ta bort medlem</p>
		<form action='' method='post'>
		<p>Användarnamn: <input type='text' name ='".LoginView::USERNAME."'/></p>
		<p>Lösenord: <input type='password' name ='".LoginView::PASSWORD."'/></p>
		<input type='submit' name='$this->m_deleteMember' value = 'Ta bort medlem'  onclick='showconfirmbox('Vill du verkligen ta bort medlemmen?')'/>
		</form>
		</div> ";
	}
	
	
	//Returns feedback on deleting member
	public function DeleteMemberSuccess($a_userName) {
		return "<p>Du har nu tagit bort medlemmen $a_userName</p>";
	}
	
	//Returns feedback for a fail on deleting member
	public function DeleteMemberFail($a_userName) {
		return "<p>Fel lösenord för användaren $a_userName, försök igen</p>";
	}
	
	//Returns feedback if the member exists
	public function MemberNotExist (){
		return "<p>Medlemmen du försöker ta bort finns inte</p>";
	}
	
	//Returns true or false on if the user have tried to delete member
	public function TriedToDeleteMember() {
		if (isset($_POST[$this -> m_deleteMember])) {
			return true;
		} else {
			return false;
		}
	}
	
	//Returns feedback if the memeber has tried to become member 
	public function TriedToBecomeMember() {
		if (isset($_POST[$this -> m_beMember])) {
			return true;
		} else {
			return false;
		}
	}
	
	public function YouHaveToLogInMessage(){
		return "<p>Du måste logga in för att kunna ta bort en medlem</>";
	}

}
